<template>
    <div class="user-profile">
      <!-- 导航栏 -->
      <van-nav-bar
        class="page-nav-bar"
        title="个人信息"
        left-arrow
        @click-left="$router.back()"
      />
      <!-- /导航栏 -->
      <input  type="file" hidden ref="file" @change="onChange" />
      <!-- 个人信息 -->
      <van-cell class="avatar-cell" title="头像" is-link center @click="$refs.file.click()">
        <van-image
          class="avatar"
          round
          fit="cover"
          :src="userInfo.photo"
        />
      </van-cell>
      <van-cell title="昵称" :value="userInfo.name" is-link @click="isUpdateNameShow = true"/>
      <van-cell title="性别" :value="userInfo.gender === 0 ? '男' :'女' "  @click="isUpdateGenderShow = true" is-link />
      <van-cell title="生日" :value="userInfo.birthday" is-link   @click="isUpdateBirthdayShow = true" />
      <!-- /个人信息 -->
      <!--修改昵称弹窗-->
      <van-popup
      v-model="isUpdateNameShow"
      style="height: 100%;"
      position="bottom"
    >
      <update-name
        v-if="isUpdateNameShow"
        v-model="userInfo.name"
        @close="isUpdateNameShow = false"
      />
    </van-popup>
     <!--修改昵称弹窗-->

    <!--修改性别弹窗-->
    <van-popup
      v-model="isUpdateGenderShow"
      style="height: 100%;"
      position="bottom"
    >
    <update-gender
    v-if="isUpdateGenderShow"
    v-model="userInfo.gender"
    @close="isUpdateGenderShow = false"
    ></update-gender>
    </van-popup>
     <!--修改性别弹窗-->

      <!--修改生日弹窗-->
    <van-popup
      v-model="isUpdateBirthdayShow"
      style="height: 100%;"
      position="bottom"
    >
    <update-birthday
    v-if="isUpdateBirthdayShow"
    v-model="userInfo.birthday"
    @close="isUpdateBirthdayShow = false"
    ></update-birthday>
    </van-popup>
    <!--修改生日弹窗-->
     <!--修改图像弹窗-->
     <van-popup
      v-model="isUpdatePhotoShow"
      style="height: 100%;"
      position="bottom"
    >
     <update-photo
     v-if="isUpdatePhotoShow"
      :img="img"
      @update-photo="userInfo.photo = $event"
      @close="isUpdatePhotoShow = false"
     ></update-photo>
    </van-popup>
     <!--修改图像弹窗-->
    </div>
  </template>

<script>
import updatePhoto from './components/update-photo.vue'
import updateBirthday from './components/update-birthday.vue'
import updateName from './components/update-name.vue'
import updateGender from './components/update-gender.vue'
import { getUserProfile } from '@/api/user'
export default {
  name: 'UserProfile',
  components: {
    updateName,
    updateGender,
    updateBirthday,
    updatePhoto
  },
  props: {},
  data () {
    return {
      userInfo: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      img: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadUserProfile()
  },
  mounted () {},
  methods: {
    onChange () {
      const file = this.$refs.file.files[0]
      this.img = window.URL.createObjectURL(file)
      this.$refs.file.value = ''
      this.isUpdatePhotoShow = true
    },
    async loadUserProfile () {
      try {
        const { data } = await getUserProfile()
        this.userInfo = data.data
      } catch (err) {
        window.console.log(err)
      }
    }
  }
}
</script>

  <style scoped lang="less">
  .user-profile {
    .avatar-cell {
      .van-cell__value {
        display: flex;
        flex-direction: row-reverse;
      }
      .avatar {
        width: 60px;
        height: 60px;
      }
    }
  }
  </style>
